package com.nb.changecolortext;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

/**
 * Version 1.0
 * Describe:自定义变色TextView
 */
class ChangeColorText extends AppCompatTextView {
    private Paint mDefaultPaint;//默认颜色
    private Paint mChangePaint;//要变的颜色
    private int mDefaultColor = Color.GRAY;
    private int mChangeColor = Color.RED;
    private float mProcess = 0.0f;
    //方向
    private Direction mDirection = Direction.RIGHT_TO_LEFT;

    public enum Direction {
        LEFT_TO_RIGHT,
        RIGHT_TO_LEFT
    }

    public ChangeColorText(Context context) {
        this(context, null);
    }

    public ChangeColorText(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ChangeColorText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ChangeColorText);
        mDefaultColor = array.getColor(R.styleable.ChangeColorText_defaultColor, mDefaultColor);
        mChangeColor = array.getColor(R.styleable.ChangeColorText_changeColor, mChangeColor);
        array.recycle();
        mDefaultPaint = getMyPaint(mDefaultColor);
        mChangePaint = getMyPaint(mChangeColor);
    }

    /**
     * 设置画笔
     * @param color
     * @return
     */
    private Paint getMyPaint(int color) {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setColor(color);
        paint.setTextSize(getTextSize());
        return paint;
    }

    /**
     * 思路 裁剪画布，左侧画布对应一个画笔，右侧画布对应一个画笔
     *
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
        int middle = (int) (mProcess * getWidth());
        if (mDirection == Direction.LEFT_TO_RIGHT) {
            //变色区域
            drawText(canvas, 0, middle, mChangePaint);
            //默认区域
            drawText(canvas, middle, getWidth(), mDefaultPaint);
        } else {
            //默认区域
            drawText(canvas, 0, getWidth() - middle, mDefaultPaint);
            //变色区域
            drawText(canvas, getWidth() - middle, getWidth(), mChangePaint);
        }


    }

    //设置方向
    public void setDirection(Direction direction) {
        this.mDirection = direction;
    }

    //设置进度
    public void setProcess(float process) {
        this.mProcess = process;
        invalidate();
    }

    public void setDefaultColor(int defaultColor) {
        this.mDefaultPaint.setColor(defaultColor);
    }

    public void setChangeColor(int changeColor) {
        this.mChangePaint.setColor(changeColor);
    }

    /**
     * 裁剪画布->画文字
     *
     * @param canvas
     * @param start
     * @param end
     * @param paint
     */
    private void drawText(Canvas canvas, int start, int end, Paint paint) {
        canvas.save();
        //裁剪画布
        canvas.clipRect(start, 0, end, getHeight());
        String text = getText().toString();

        //获取文字起点
        Rect bounds = new Rect();
        paint.getTextBounds(text, 0, text.length(), bounds);
        int x = getWidth() / 2 - bounds.width() / 2;

        //获取基线
        Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();
        int dy = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
        int baseLine = getHeight() / 2 + dy;

        canvas.drawText(text, x, baseLine, paint);
        canvas.restore();

    }
}
